//
//  BKVistorView.swift
//  微博
//
//  Created by mac on 16/8/9.
//  Copyright © 2016年 xbk. All rights reserved.
//

import UIKit
import SnapKit

class BKVistorView: UIView {

    var loginClosure: (()->())?
    
    //  MARK: -- 懒加载控件
    private lazy var cycleImageView :UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    private lazy var maskImageView: UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var iconImageView: UIImageView = UIImageView(image:UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var messageLabel: UILabel = {
    
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(24)
        label.textAlignment = .Center
        label.numberOfLines = 0
        
        return label
    }()
    
    //注册
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        button.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
        
        return button
    }()
    
    //登录
    private lazy var loginButton:UIButton = {
        
        let button = UIButton()
        
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        button.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        
        return button
    }()
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func setupUI(){
    
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //设置约束
        cycleImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(cycleImageView.snp_bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.leading.equalTo(messageLabel)
            make.height.equalTo(35)
            make.width.equalTo(100)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }
    
    }
    
    //开启动画
    private func startAnimation() {
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        cycleImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    
    
    //修改访客视图信息
    func updateVisitorViewInfo(message:String?,imageName:String?) {
    
        if let  msg  = message , imgName = imageName{
        
            messageLabel.text = msg
            iconImageView.image = UIImage(named:imgName)
            cycleImageView.hidden = true
        }else{
        
            cycleImageView.hidden = false
            startAnimation()
        }
    }
    
    
    func registerAction() {
    
        print("注册")
        loginClosure?()
    }
    
    func loginAction () {
    
        print("登录")
        loginClosure?()
    }
    
}
